package middle_ware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-base/src/api/response"
	"go-base/src/logs"
)

//Recovery 捕获异常
func Recovery() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				//发送日志
				logs.Error.Printf("捕获异常，err:%v\n", err)
				response.HttpReturn(c, response.RESPONSE_UNKNOWN, fmt.Sprintf("%s", err), "服务器错误")
				c.Abort()
				return
			}
		}()
		c.Next()
	}
}
